﻿@{
    string _prefix = "org";
    var _treeId = _prefix + "Tree";
    var _gridId = _prefix + "Grid";
    var _treeDetail = _prefix + "Detail";
}
<div class="bjui-pageContent">
    <div class="clearfix">
        <div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
            <ul id="@_treeId" class="ztree"></ul>
        </div>

        <div id="@_treeDetail" style="margin-left: 225px; width: auto;height: auto">
            <table id="@_gridId" data-width="100%" data-height="100%" class="table table-bordered"></table>
        </div>
    </div>
</div>

<script type="text/javascript">
    var selectedId = 0;
    $(document).ready(function () {
        Init(0);
    });
    //加载数据到datagrid
    function LoadOrg(data) {
        //b-jui的datagrid需要重新处理HTML
        $('#@_treeDetail').empty()
            .append('<table id="@_gridId" data-width="100%" data-height="100%" class="table table-bordered"></table>');
        $('#@_gridId').datagrid({
            gridTitle: '机构列表显示',
            showToolbar: true,
            filterThead: false,
            toolbarItem: 'refresh, |, del,edit',
            toolbarCustom: '<a href="/OrgManager/AddOrg" class="btn btn-green" data-icon ="plus"  ' +
                'data-toggle="dialog" data-id="dialog-mask" data-mask="true">添加</a>' ,
            columns: [
            {
                name: 'Id',
                label: 'Id',
                attrs: { readonly: 'readonly' },
                hide: true
            },
            {
                name: 'ParentId',
                label: '上级机构ID',
                attrs: { readonly: 'readonly'},
                hide: true
            },
              {
                  name: 'CascadeId',
                  label: '唯一标识',
                  attrs: { readonly: 'readonly'}
              },
              {
                  name: 'Name',
                  label: '机构名称'
              },
              {
                  name: 'ParentName',
                  label: '上级机构',
                  edit: false,
                  attrs: { readonly: 'readonly' }
              },
            {
                name: 'Status',
                label: '状态',
                type: 'select',
                align: 'center',
                items:[{'0':'正常'}, {'1':'禁用'}]
            },
              {
                  name: 'CreateTime',
                  label: '登记日期',
                  type: 'date',
                  pattern: 'yyyy-MM-dd HH:mm:ss'
              }
            ],
            data: data,
            delUrl: 'OrgManager/DelOrg',
            delPK: "Id",
            editUrl: 'OrgManager/EditOrg',
            editMode: 'dialog',
            fullGrid: true,
            showLinenumber: true,
            showCheckboxcol: true,
            paging: false,
            filterMult: false,
            showTfoot: true,
            delCallback: function (delResult) {
                if (delResult.statusCode == "200")
                    Init(selectedId);
                else {
                    $(this).alertmsg('warn', delResult.message);
                }
            },
            editCallback: function (delResult) {
                if (delResult.statusCode == "200")
                    Init(selectedId);
                else {
                    $(this).alertmsg('warn', delResult.message);
                }
            }
        });
    }
    function zTreeOnClick(event, treeId, treeNode) {
        selectedId = treeNode.Id;
        $.getJSON('OrgManager/LoadChildren', {
            id: treeNode.Id
        }, function (json) {
            LoadOrg(json);
        });
    }

    function Init(selectedId) {
        var setting = {
            view: {
                selectedMulti: false
            },
            data: {
                key: {
                    name: 'Name',
                    title: 'Name'
                },
                simpleData: {
                    enable: true,
                    idKey: 'Id',
                    pIdKey: 'ParentId',
                    rootPId: 'null'
                }
            },
            callback: {
                onClick: zTreeOnClick
            }
        };
        $.getJSON('OrgManager/LoadOrg', function (json) {
            var zTreeObj = $.fn.zTree.init($('#@_treeId'), setting, json);
            zTreeObj.expandAll(true);

            $.getJSON('OrgManager/LoadChildren', {
                id: selectedId
            }, function (data) {
                LoadOrg(data);
            });

            //TODO：设置ztree选中，不过没看到效果..
            var selectedNod = zTreeObj.getNodesByParam('Id', selectedId, null);
            zTreeObj.selectNode(selectedNod, false);

        });
    }
    //@@ sourceURL=orgIndex.js
</script>
